<?php 
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

defined('_JEXEC') or die('Restricted access');

/**
 * @package		Joomla
 * @subpackage	ja_profiles
 */
class JAJobBoardControllerJATransactions extends JAJobBoardController 
{


     /* Constructor
     */
    function __construct(  )
    {    	
        parent::__construct( ); 
        checkPermission_Administrator('transactions'); 
        JRequest::setVar('name','JATransactions');     
        // Register Extra tasks
        $this->registerTask( 'apply',   'save' );        
		$this->registerTask( 'add', 	'edit');
		$this->registerTask( 'drop', 	'remove');
    }


    function display(){
    	global $jbconfig;
		$task = JRequest::getVar('task');
		$pre_task = JRequest::getVar('pre_task');
		$layout = JRequest::getVar('layout','default');
		switch($layout)
		{
			case 'form'     :
			{
				JRequest::setVar( 'edit', true );
				
			} break;
			default:
				if ($task=='add')
				{
					JRequest::setVar('task',$pre_task);
					JRequest::setVar( 'edit', false );
					JRequest::setVar( 'layout', 'form' );
				}
				
			
		}    	
		
		parent::display();
    }
    
    function edit()
    {
    	$task = JRequest::getVar('pre_task');
    	JRequest::setVar( 'edit', true );
		JRequest::setVar( 'layout', 'form' );
		JRequest::setVar('task',$task);
		parent::display();
    }

    function remove()
    {
    	global $option;
    	$pre_task = JRequest::getVar('pre_task');
    	$user_id = JRequest::getVar('userid');
    	$model =& $this->getModel('jatransactions');
        if(($n = $model->remove()) < 0)
             JError::raiseWarning( 500, $row->getError() );
		else 
			$this->setMessage(JText::sprintf('Items removed', $n));
		$this->setRedirect("index.php?option=$option&view=jatransactions&task=$pre_task&userid=$user_id");    	
    }
    
    function cancel(){
    	global $option;
    	$pre_task = JRequest::getVar('pre_task');
    	$user_id = JRequest::getVar('userid');
        $this->setRedirect("index.php?option=$option&view=jatransactions&task=$pre_task&userid=$user_id");
    } 
    
    function revert()
    {
    	
    	$product_id = JRequest::getVar('product_id');
    	$product_type = JRequest::getVar('product_type');
    	$pre_task = JRequest::getVar('pre_task');
    	$user_id = JRequest::getVar('userid');
    	$task = JRequest::getVar('task');
    	
    	global $option;
    	$model = $this->getModel('JATransactions');
    	$row = $model->getItem();
    	$model->revertTransaction($user_id,$product_id,$product_type,0,$row);
    	$msg = JText::_("Successfully reversed transaction");
    	$this->setRedirect("index.php?option=$option&view=jatransactions&task=$pre_task&userid=$user_id",$msg);
    }
    
    function save()
    {
    	global $option;
    	
    	$pre_task = JRequest::getVar('pre_task');
    	$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
    	$user_id = JRequest::getVar('userid');
    	$task = JRequest::getVar('task');
    	
    	$model = $this->getModel('JATransactions');
    	if ($rs=$model->store())
    	{
    		if (($cid[0]!=0)&&(isset($cid[0])))
				$msg = JText::_( 'Successfully updated transaction' );
			else 
				$msg = JText::_( 'Successfully created transaction' );
    		if ($task == 'save')
    		{
    			$this->setRedirect("index.php?option=$option&view=jatransactions&task=$pre_task&userid=$user_id",$msg);
    		}
    		elseif ($task == 'apply')
    		{
    			$this->setRedirect("index.php?option=$option&view=jatransactions&layout=form&cid[]=$rs&task=$pre_task&userid=$user_id",$msg);
    		}
    	}
    }    
    
	function convert_pdf()
	{
		global $option;
		
		$tranid = JRequest::getVar('tranid');
		$type = JRequest::getVar('type','');
		if(($tranid !=0)&&($type=='invoice'))
		{
			$url  = "index2.php?option=$option&view=jatransactions&task=print_invoice&ajax=1";
			$url .= "&tranid=$tranid&format=pdf";
		}
		else 
		{
			$cid = JRequest::getVar('cid');
			$url  = "index2.php?option=$option&view=jatransactions&task=view_trans&ajax=1";
			$url .= "&cid=$cid&format=pdf";
		}
		$this->setRedirect($url);
	}
	function back_profile()
	{
		global $option;
		
		$userid=JRequest::getVar('userid',null);
		$msg = JText::_( 'OPERRATION CANCELLED' );
		if($userid==null)
			$this->setRedirect( "index.php?option=$option&view=ja_profiles", $msg );
		else {
			$this->setRedirect( "index.php?option=$option&view=ja_profiles&task=user&user_id=$userid", $msg );			
		}
	}
	
	function check_user()
	{
		$model = $this->getModel('JATransactions');
		if ($model->check_user())
			echo 'Yeah';
		else 	
			echo JText::_('This user is not exist');
		exit;
	}
	function show_product_object_list()
	{
		$view = $this->getView('JATransactions','html');
		$obj = $view->show_dropdown_produt_list(JRequest::getVar('product_type'));
		echo $obj;
		exit;
	}
	
	function show_product_info()
	{
		$model = $this->getModel('JATransactions');
		$info = $model->getProductInfo();
		echo $info;
		exit;
	}
	
}
?>